package com.sc.admin.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sc.admin.common.ResponseResult;
import com.sc.admin.pojo.Menu;
import com.sc.admin.service.MenuService;

@Controller
@RequestMapping("/menu")
public class MenuController {
	@Autowired
	private MenuService menuService;

	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String menuPage() {
		return "menu-list";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String addPage() {
		return "menu-add";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public @ResponseBody Object add(Menu menu) {
		menuService.add(menu);
		return new ResponseResult().ok(menu);
	}
	
	@RequestMapping(value="/name/check",method=RequestMethod.GET)
	public @ResponseBody Object check(String name) {
		Menu menu = menuService.checkName(name);
		return new ResponseResult().ok(menu);
	}
	
	@RequestMapping(value="/edit/{id}",method=RequestMethod.GET)
	public String editPage(@PathVariable int id,Model model) {
		Menu menu = menuService.getMenuById(id);
		model.addAttribute(menu);
		return "menu-edit";
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public @ResponseBody Object edit(Menu menu) {
		menuService.update(menu);
		return new ResponseResult().ok();
	}
	
	@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)
	public @ResponseBody Object delete(@PathVariable int id) {
		menuService.delete(id);
		return new ResponseResult().ok();
	}
}
